package com.andaily.domain.dto.user;

import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.shared.DateUtils;
import com.andaily.domain.shared.PasswordHandler;
import com.andaily.domain.user.User;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Shengzhao Li
 * @see com.andaily.web.controller.mobile.user.UserFormController
 */
public class UserFormDTO extends AbstractDTO {

    //Should be unique.
    private String email;
    private String city;

    private String nickName;
    private String cellPhone;
    private String oldCellPhone;
    private String lastLoginTime;

    private String oldPassword;
    private String newPassword;
    private String confirmPassword;

    public UserFormDTO() {
    }

    public UserFormDTO(User user) {
        this.guid = user.guid();
        this.email = user.email();
        this.city = user.city();

        this.nickName = user.nickName();
        this.cellPhone = user.cellPhone();
        this.oldCellPhone = this.cellPhone;
        this.lastLoginTime = DateUtils.toDateTime(user.lastLoginTime());
    }

    public String getLastLoginTime() {
        if (StringUtils.isEmpty(lastLoginTime)) {
            return "无";
        }
        return lastLoginTime;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getCellPhone() {
        return cellPhone;
    }

    public void setCellPhone(String cellPhone) {
        this.cellPhone = cellPhone;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    @Override
    public String toString() {
        return "UserFormDTO{" +
                "email='" + email + '\'' +
                ", city='" + city + '\'' +
                ", nickName='" + nickName + '\'' +
                ", cellPhone='" + cellPhone + '\'' +
                '}';
    }

    public Map<String, Object> updateMap(String userGuid) {
        Map<String, Object> updateMap = new HashMap<String, Object>();
        updateMap.put("guid", userGuid);
        updateMap.put("city", city);
        updateMap.put("nickName", nickName);

        String phone = StringUtils.isEmpty(cellPhone) ? null : cellPhone;
        updateMap.put("cellPhone", phone);
        if (StringUtils.isNotEmpty(newPassword)) {
            String encryptPass = PasswordHandler.encryptPassword(newPassword);
            updateMap.put("newPassword", encryptPass);
        }
        return updateMap;
    }

    public String getOldCellPhone() {
        return oldCellPhone;
    }

    public void setOldCellPhone(String oldCellPhone) {
        this.oldCellPhone = oldCellPhone;
    }
}
